$(document).ready(function(){
    var str = "http://localhost:8080/Fish/picture/";
	function show(){
		if(images.length==0) {alert("未上传照片");return;}
		$("#showAll").css({"display":"block"});
        $("#imgDiv").css({"display":"inline-block"});

		console.log(images);
		console.log(str+selectID+"/"+images[0]);
		$("#img1").attr("src",str+selectID+"/"+images[0]);
	}
	var trArray = ["a","b","c","d","e"];
	$(document).on("click","#fishedButton0,#fishedButton1,#fishedButton2,#fishedButton3,#fishedButton4",function(){
		var substring = $(this).attr("ID").substring(12,13);
		var index ;
	    index = Number(substring);
		var selectTr = trArray[index];
		var ID  = $("#fishesDiv #"+selectTr+" td:first-child").html();
		console.log("                  234 "+ID);
		if(ID.length == 0) return; 
		 $.ajax({
		   	  type:"get",
			  async:true,
			  url:"op_fish.action",
			  data:{
				  "flag":"getAllPhoto",
				  "NodeName":ID,								//主键
				 
			  },
			  success:function(result){
				  if(result.length==0) images=[];
				  else images = result.split("|");
				  console.log(result+"               "+images);
				  selectID = ID;
				  show();
			  }
	   });
		
	});
	
	$(document).on("click","#fishEggsButton0,#fishEggsButton1,#fishEggsButton2,#fishEggsButton3,#fishEggsButton4",function(){
		console.log("ttttttttttttttttt");
		var substring = $(this).attr("ID").substring(14,15);
		var index ;
	    index = Number(substring);
		var selectTr = trArray[index];
		var ID  = $("#FishEggsDiv #"+selectTr+" td:first-child").html();
		console.log("                  234 "+ID);
		if(ID.length == 0) return; 
		 $.ajax({
		   	  type:"get",
			  async:true,
			  url:"op_egg.action",
			  data:{
				  "flag":"getAllPhoto",
				  "NodeName":ID,								//主键
				 
			  },
			  success:function(result){
				  if(result.length==0) images=[];
				  else images = result.split("|");
				  console.log(result+"               "+images);
				  selectID = ID;
				  show();
			  }
	   });
		
	});
	
	
	
	
	$(document).on("click","#catchesButton,#catchesButton1,#catchesButton2,#catchesButton3,#catchesButton4",function(){
		var substring = $(this).attr("ID").substring(13,14);
		var index ;
		if(substring == null) index=0;
		else index = Number(substring);
		var selectTr = trArray[index];
		var ID  = $("#"+selectTr+" td:first-child").html();
		if(ID.length == 0) return; 
		  $.ajax({
		   	  type:"get",
			  async:true,
			  url:"op_catch.action",
			  data:{
				  "flag":"getAllPhoto",
				  "NodeName":ID,								//主键
				 
			  },
			  success:function(result){
				  if(result.length==0) images=[];
				  else images = result.split("|");
				  console.log(result+"               "+images);
				  selectID = ID;
				  show();

			  }
	   });
	});
	
	$("#toolButton").click(function(){
		images = toolImages;
		selectID = $(".toolID").html();
		show();
	});
	
	
	$("#getButton").click(function(){
		show();
	});
	
	
	$("#eggButton").click(function(){
		show();
	});
	
	$("#fishesButton").click(function(){
		show();
	});
	
	
	$("#monitsiteButton").click(function(){
		show();
	});

     var currnt = 0;
    $("#last").click(function () {
            var src;
            if(currnt == 0) currnt = images.length-1;
            else currnt--;
            $("#img1").attr("src",str+selectID+"/"+images[currnt]+"?t=" + Math.random());
     });

     $("#next").click(function(){
         if(currnt == images.length-1) currnt=0;
         else currnt++;
         $("#img1").attr("src",str+selectID+"/"+images[currnt]+"?t=" + Math.random());

     });
     $("#close").click(function(){
         $("#showAll").css({"display":"none"});
         $("#imgDiv").css({"display":"none"});

     });
     
     $("#deleteSelect").click(function(){
    	   var first = selectID.substring(0,3);
      	   var url;
      	   var formData={};
	           formData["flag"]="delete";
		       formData["NodeName"]=selectID;
		       formData["deletePhoto"]=images[currnt];
      	   switch(first){
	   			case "NET":  url="op_catools.action";     
                             break;
	   			case "CTH":  url="op_catch.action"; 
	   			             break;
	   			case "FSS":  url="op_fish.action";   	
	   			             break;
	   			case "FSE":  url="op_egg.action"; 
	   			             break;
	   			case "MON":  url="op_monsite.action";
	   			             break;
      	   } 
    	 
    	 
    	 
    	 $.ajax({
		   	  type:"post",
			  async:true,
			  url: url ,
			  data:formData,
			  success:function(result){
				  if(result=="error"){
					  alert("更新失败！");
				  }else{
					  images = result.split("|");
					  alert(images);
					  console.log(images);
				      if(currnt+1 >= images.length-1) currnt=0;
				      else currnt++;
				      $("#img1").attr("src",str+selectID+"/"+images[currnt]+"?t=" + Math.random());
				  } 
			  }
	   });
    	 
    	 
     });
     
     
	
});